// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false

// 按需引入mint-ui组件
// import { Header, Swipe, SwipeItem, Button } from 'mint-ui';
// Vue.component(Header.name, Header);
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);

// 引用懒加载组件，需要全部引用mint-ui
import mintUi from 'mint-ui'
Vue.use(mintUi)
import 'mint-ui/lib/style.min.css';

// 引入mui组件
import './lib/mui/css/mui.css'

// 需要引入扩展字体图标的css
import './lib/mui/css/icons-extra.css'

// 引入axios进行数据的请求
import Axios from 'axios'
import qs from 'qs'
Vue.prototype.$http = Axios
// 添加请求拦截器
Axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  if(config.method == 'post'){
    config.data = qs.stringify(config.data)
  }
  return config;
})
  
// 配置全局的域名
Axios.defaults.baseURL = 'http://vue.studyit.io/'

// 全局配置moment时间格式化
import moment from 'moment'
Vue.filter('fordate',(data,parter='YYYY-MM-DD HH:mm:ss')=>{
  return moment(data).format(parter)
})

// 引用缩略图组件
import VuePreview from 'vue-preview'
Vue.use(VuePreview)

// 引入vuex，并注册
import Vuex from 'vuex'
Vue.use(Vuex)

var car = JSON.parse(localStorage.getItem('car') || '[]')
var store = new Vuex.Store({
  state:{
    car:car
  },
  mutations:{
    addcar(state,goodsinfo){
      var flag = false
      state.car.some(item => {
        if(item.id == goodsinfo.id){
          item.count += goodsinfo.count
          flag = true
          return true
        }
      })
      if(!flag){
        state.car.push(goodsinfo)
      }
      localStorage.setItem('car',JSON.stringify(state.car))
    },
    removegoods(state,id){
      state.car.some((item,index) => {
        if(item.id == id){
          state.car.splice(index,1)
          return true
        }
      })
      localStorage.setItem('car',JSON.stringify(state.car))
    },
    changeCounts(state,obj){
      state.car.some(item => {
        if(item.id == obj.id){
          item.count = parseInt(obj.count)
          return true
        }
      })
    },
    changeSelected(state,obj){
      state.car.some(item => {
        if(item.id == obj.id){
          item.selected = obj.status
          return true
        }
      })
      localStorage.setItem('car',JSON.stringify(state.car))
    }
  },
  getters:{
    // 实现徽标赋值
    getcount(state){
      var c = 0
      state.car.forEach(item => {
        c += item.count
      })
      return c
    },
    getGoodsCount(state){
      var obj = {}
      state.car.forEach(item => {
        obj[item.id] = item.count
      })
      return obj
    },
    // 实现总计
    getAllGoods(state){
      var obj = {price:0,count:0}
      state.car.forEach(item => {
        if(item.selected){
          obj.count += item.count
        obj.price += item.count * item.price
        }
      })
      return obj
    },
    // 实现选中状态
    getGoodsStatus(state){
      var obj = {}
      state.car.forEach(item => {
        obj[item.id] = item.selected
      })
      return obj
    }
  }
})


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
